﻿''' <summary>
''' 自定义dataGridView类，用来处理回车跳到下一列或下一行的第一列
''' </summary>
Public Class DataGridViewEnter
    Inherits DataGridView

    Private m_InEidtMode As Boolean = False

    Public Property InEidtMode As Boolean
        Get
            Return m_InEidtMode
        End Get
        Set(value As Boolean)
            m_InEidtMode = value
        End Set
    End Property

    ''' <summary>
    ''' 处理enterKey
    ''' </summary>
    ''' <param name="keyData"></param>
    ''' <returns></returns>
    Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean

        'Debug.Print("ProcessDialogKey发生，keydata=" & keyData.ToString())
        ' Extract the key code from the key value. 
        '取出当前键值
        Dim key As Keys = keyData And Keys.KeyCode

        ' Handle the ENTER key as if it were a RIGHT ARROW key. 
        '如果是回车，则当成右键处理
        If key = Keys.Enter Then
            Return Me.ProcessTabKey(keyData)
            'Return Me.ProcessRightKey(keyData)
        End If

        '
        Return MyBase.ProcessDialogKey(keyData)
    End Function

    Protected Overrides Function ProcessDataGridViewKey(e As KeyEventArgs) As Boolean
        'Debug.Print("ProcessDataGridViewKey发生，keyCode=" & e.KeyCode.ToString() & "keyData=" & e.KeyData.ToString())
        '取出当前键值 
        If e.KeyCode = Keys.Enter Then
            '判断是否需要新增一行
            If Me.InEidtMode AndAlso Me.CurrentCell.ColumnIndex = Me.Columns.GetColumnCount(DataGridViewElementStates.Visible) - 1 AndAlso Me.CurrentRow.Index = Me.Rows.Count - 1 Then
                '这里新增行
                Me.Rows.Add()
            End If
            '处理跳格
            Return Me.ProcessTabKey(e.KeyData)

            End If
            '
            Return MyBase.ProcessDataGridViewKey(e)
    End Function
End Class
